package opera;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ken
 * Date: 2023-11-24
 * Time: 18:27
 */
public class DelOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
        System.out.println("请输入你要删除的书名");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int currentSize = bookList.getUsedSize();
        int index = -1;
        for(int i = 0 ; i <currentSize; i++) {
            Book temp = bookList.getBook(i);
            if (temp.getName().equals(name)) {
                index = i;
                break;
            }
        }
            for (int j = index; j < currentSize-1; j++) {
                Book book = bookList.getBook(j+1);
                bookList.setBooks(j,book);
            }
            bookList.setBooks(currentSize-1,null);
            bookList.setUsedSize(currentSize-1);

        System.out.println("删除成功");
    }
}
